package cn.moningxi.service.impl;

import cn.moningxi.bean.GoodsInfomation;
import cn.moningxi.mapper.GGoodsReportMapper;
import cn.moningxi.mapper.GPictureMapper;
import cn.moningxi.mapper.GoodsInfomationMapper;
import cn.moningxi.service.GoodsInfomationService;
import cn.moningxi.util.CustomIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsInfomationServiceImpl implements GoodsInfomationService {
    @Autowired
    GoodsInfomationMapper goodsInfomationMapper;

    @Autowired
    GPictureMapper gPictureMapper;

    @Autowired
    GGoodsReportMapper gGoodsReportMapper;


    @Override
    public List<GoodsInfomation> queryGoodsByName(String keywords) {
        return goodsInfomationMapper.queryGoodsByName(keywords);
    }

    @Override
    public List<GoodsInfomation> queryGoodsByCategoryId(Integer categoryId) {
        return goodsInfomationMapper.queryGoodsByCategoryId(categoryId);
    }

    @Override
    public GoodsInfomation queryGoodsById(String goodsId) {
        return goodsInfomationMapper.queryGoodsById(goodsId);
    }

    @Override
    public List<GoodsInfomation> selectAll() {
        return goodsInfomationMapper.selectAll();
    }

    @Override
    public List<GoodsInfomation> seacherGoodsByName(String nameInfo) {
        return goodsInfomationMapper.seacherGoodsByName(nameInfo);
    }

    @Override
    public void updateGoodsInfo(GoodsInfomation goodsInfomation) {
        goodsInfomationMapper.updateGoodsInfo(goodsInfomation);
    }

    @Override
    public GoodsInfomation queryByGoodaSn(String goodsSn) {
        return goodsInfomationMapper.queryByGoodaSn(goodsSn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGoods(GoodsInfomation goodsInfomation) {
        goodsInfomation.setCreateTime(LocalDateTime.now());
        goodsInfomation.setGoodsId(String.valueOf(new CustomIdGenerator().nextId(GoodsInfomationServiceImpl.class)));
        String goodsId = goodsInfomation.getGoodsId();
        String imgs = "business/images/imgsearch1.jpg";
        gPictureMapper.add(goodsId, imgs);
        gGoodsReportMapper.addGoodsReport(goodsId);
        goodsInfomationMapper.addGoods(goodsInfomation);
    }
}
